﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetTop5ConfirmedTask]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetTop5ConfirmedTask];
GO
CREATE PROCEDURE [dbo].[sproc_GetTop5ConfirmedTask]
    @Date datetime,
             @Username nvarchar(20)

As

--select top 5 a.taskid  
--            from uds_schedule a  
--            group by a.taskid,username,date having username=@Username and [date]=@Date and count(isconfirm)=sum(isconfirm) order by taskid asc
select distinct top 5 a.id,count(isconfirm),sum(isconfirm) from uds_task a,uds_schedule b  where a.id=b.taskid and a.Attribute=1 and  (
a.arrangedby=@Username or username=@username ) and date=@Date group by a.id order by a.id desc